package com.triones.api.service.interlace.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.triones.api.entity.interlace.bo.InterlaceTransfersParams;
import com.triones.api.entity.interlace.dto.InterlaceTransfersFeesDO;
import com.triones.api.entity.interlace.vo.CustomInterlaceTransfersVO;
import com.triones.api.entity.interlace.vo.InterlaceTransfersFeesVO;
import com.triones.api.entity.interlace.vo.InterlaceTransfersVO;
import com.triones.api.mapper.interlace.InterlaceTransfersFeesMapper;
import com.triones.api.mapper.interlace.InterlaceTransfersMapper;
import com.triones.api.service.interlace.CryptoTransfersService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * interlace加密货币交易管理
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class CryptoTransfersServiceImpl implements CryptoTransfersService {

    private final InterlaceTransfersMapper interlaceTransfersMapper;

    private final InterlaceTransfersFeesMapper interlaceTransfersFeesMapper;

    @Override
    public TableDataInfo<CustomInterlaceTransfersVO> assetsTransfersPageList(InterlaceTransfersParams transfersParams, PageQuery pageQuery) {
        Map<String, Object> params = transfersParams.getParams();
        QueryWrapper<Object> wrapper = Wrappers.query();
        if (transfersParams.getScene() == null) {
            wrapper.in("tit.scene", "crypto_assets_out", "crypto_assets_in", "assets_withdrawal", "assets_deposit");
        } else {
            if ("assets_out".equals(transfersParams.getScene())) {
                wrapper.in("tit.scene", "crypto_assets_out", "assets_withdrawal");
            }
            if ("assets_in".equals(transfersParams.getScene())) {
                wrapper.in("tit.scene", "crypto_assets_in", "assets_deposit");
            }
        }
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getSn()), "tit.sn", transfersParams.getSn());
        wrapper.and(StringUtils.isNotEmpty(transfersParams.getMember()), qr ->
            qr.like("tim.first_name", transfersParams.getMember()).or()
                .like("tim.last_name", transfersParams.getMember()).or()
                .like("tim.email", transfersParams.getMember()).or()
                .like("tim.user_no", transfersParams.getMember()).or()
                .like("tim.nickname", transfersParams.getMember())
        );
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getStatus()), "tit.status", transfersParams.getStatus());
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getCurrency()), "tit.destination_currency", transfersParams.getCurrency());
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getChain()), "tit.destination_chain", transfersParams.getChain());
        wrapper.between(params.get("beginTime") != null && params.get("endTime") != null,
            "tit.create_time", params.get("beginTime"), params.get("endTime"));
        wrapper.orderByDesc("tit.create_time");
        Page<CustomInterlaceTransfersVO> page = interlaceTransfersMapper.selectAssetsTransfersPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(page);
    }

    @Override
    public CustomInterlaceTransfersVO cryptoTransfersDetail(Long id) {
        CustomInterlaceTransfersVO vo = new CustomInterlaceTransfersVO();
        InterlaceTransfersVO transferVO = interlaceTransfersMapper.selectVoById(id);
        if (transferVO != null) {
            BeanUtil.copyProperties(transferVO, vo);
        }
        List<InterlaceTransfersFeesVO> feesVOS = interlaceTransfersFeesMapper.selectVoList(new LambdaQueryWrapper<InterlaceTransfersFeesDO>()
            .eq(InterlaceTransfersFeesDO::getTid, id));
        vo.setFees(feesVOS);
        return vo;
    }

    @Override
    public TableDataInfo<CustomInterlaceTransfersVO> quoteTransfersPageList(InterlaceTransfersParams transfersParams, PageQuery pageQuery) {
        Map<String, Object> params = transfersParams.getParams();
        QueryWrapper<Object> wrapper = Wrappers.query();
        wrapper.ne("tim.id", 1);
        if (transfersParams.getScene() == null) {
            wrapper.in("tit.scene", "buy", "sell");
        } else {
            wrapper.eq("tit.scene",transfersParams.getScene());
        }
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getSn()), "tit.sn", transfersParams.getSn());
        wrapper.and(StringUtils.isNotEmpty(transfersParams.getMember()), qr ->
            qr.like("tim.first_name", transfersParams.getMember()).or()
                .like("tim.last_name", transfersParams.getMember()).or()
                .like("tim.email", transfersParams.getMember()).or()
                .like("tim.user_no", transfersParams.getMember()).or()
                .like("tim.nickname", transfersParams.getMember())
        );
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getStatus()), "tit.status", transfersParams.getStatus());
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getSourceCurrency()), "tit.source_currency", transfersParams.getSourceCurrency());
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getDestinationCurrency()), "tit.destination_currency", transfersParams.getDestinationCurrency());
        wrapper.eq(StringUtils.isNotEmpty(transfersParams.getRfqCurrency()), "tit.rfq_currency", transfersParams.getRfqCurrency());
        wrapper.between(params.get("beginTime") != null && params.get("endTime") != null,
            "tit.create_time", params.get("beginTime"), params.get("endTime"));
        wrapper.orderByDesc("tit.create_time");
        Page<CustomInterlaceTransfersVO> page = interlaceTransfersMapper.selectAssetsTransfersPageList(pageQuery.build(), wrapper);
        return TableDataInfo.build(page);
    }
}
